package com.accloud.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import com.accloud.cloudservice.AC;
import com.accloud.cloudservice.VoidCallback;
import com.accloud.service.ACException;
import cz.msebera.android.httpclient.protocol.HttpRequestExecutor;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class ACUtils {
    private static final String TAG = ACUtils.class.getSimpleName();

    public static void isNetworkConnected(final VoidCallback callback) {
        final Handler handler = new Handler() {
            /* class com.accloud.utils.ACUtils.AnonymousClass1 */

            @Override // android.os.Handler
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 0:
                        callback.error(new ACException(ACException.NO_NETWORK_AVAILABLE, "no network available"));
                        return;
                    case 1:
                        callback.success();
                        return;
                    default:
                        return;
                }
            }
        };
        new Thread(new Runnable() {
            /* class com.accloud.utils.ACUtils.AnonymousClass2 */

            @Override // java.lang.Runnable
            public void run() {
                NetworkInfo netInfo = ((ConnectivityManager) AC.context.getSystemService("connectivity")).getActiveNetworkInfo();
                if (netInfo != null && netInfo.isConnected()) {
                    try {
                        HttpURLConnection urlConnection = (HttpURLConnection) new URL("http://www.baidu.com").openConnection();
                        urlConnection.setConnectTimeout(HttpRequestExecutor.DEFAULT_WAIT_FOR_CONTINUE);
                        urlConnection.connect();
                        if (urlConnection.getResponseCode() == 200) {
                            handler.sendEmptyMessage(1);
                            return;
                        }
                    } catch (IOException e) {
                    }
                }
                handler.sendEmptyMessage(0);
            }
        }).start();
    }

    public static boolean isNetworkConnected() {
        ConnectivityManager mConnectivityManager;
        NetworkInfo mNetworkInfo;
        if (AC.context == null || (mConnectivityManager = (ConnectivityManager) AC.context.getSystemService("connectivity")) == null || (mNetworkInfo = mConnectivityManager.getActiveNetworkInfo()) == null) {
            return false;
        }
        return mNetworkInfo.isAvailable();
    }

    public static boolean isWifiConnected() {
        ConnectivityManager mConnectivityManager;
        NetworkInfo wifiNetworkInfo;
        return (AC.context == null || (mConnectivityManager = (ConnectivityManager) AC.context.getSystemService("connectivity")) == null || (wifiNetworkInfo = mConnectivityManager.getNetworkInfo(1)) == null || !wifiNetworkInfo.isConnected()) ? false : true;
    }

    public static byte[] inToBytes(InputStream in) throws IOException {
        if (in == null) {
            return null;
        }
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        while (true) {
            int ch = in.read();
            if (ch != -1) {
                byteStream.write(ch);
            } else {
                byte[] byteArray = byteStream.toByteArray();
                in.close();
                byteStream.close();
                return byteArray;
            }
        }
    }

    public static File createSDDir(String dirName) {
        File dir = new File((Environment.getExternalStorageDirectory() + "/") + dirName);
        dir.mkdir();
        return dir;
    }

    public static File createSDFile(String fileName) throws IOException {
        File file = new File((Environment.getExternalStorageDirectory() + "/") + fileName);
        file.createNewFile();
        return file;
    }

    public static String getPhoneId(Context context) {
        try {
            String deviceId = getImei(context);
            if (deviceId != null) {
                return deviceId;
            }
            String deviceId2 = getMacAddress(context);
            if (deviceId2 == null || deviceId2.length() <= 0 || deviceId2.equals("02:00:00:00:00:00")) {
                return "" + (Build.BOARD.length() % 10) + (Build.BRAND.length() % 10) + (Build.CPU_ABI.length() % 10) + (Build.DEVICE.length() % 10) + (Build.DISPLAY.length() % 10) + (Build.HOST.length() % 10) + (Build.ID.length() % 10) + (Build.MANUFACTURER.length() % 10) + (Build.MODEL.length() % 10) + (Build.PRODUCT.length() % 10) + (Build.TAGS.length() % 10) + (Build.TYPE.length() % 10) + (Build.USER.length() % 10);
            }
            return deviceId2;
        } catch (Exception e) {
            LogUtil.d(TAG, e.toString());
            return null;
        }
    }

    public static String getMacAddress(Context context) {
        WifiInfo info;
        WifiManager wifi = (WifiManager) context.getSystemService("wifi");
        if (wifi == null || (info = wifi.getConnectionInfo()) == null) {
            return null;
        }
        return info.getMacAddress();
    }

    public static String getImei(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService("phone");
        if (tm == null) {
            return null;
        }
        return tm.getDeviceId();
    }
}
